import json
import os

from django.http import HttpResponse
from .functions import loginclass
from utils.token import Token


# Create your views here.
def login(request):
    """
    登录接口
    :param request:
    :return:
    """
    request_data = json.loads(request.body.decode('utf-8'))
    return_data = loginclass.login(request_data['username'], request_data['password'])
    return HttpResponse(json.dumps(return_data))


def logout(request):
    """
    注销接口
    :param request:
    :return:
    """
    token = request.headers['token']
    del_token = Token()
    del_token.token = token
    delete_token = del_token.clear_token()  # 注销后删除redis中的token
    if delete_token:
        return_data = {'code': 200, "msg": "Token清除成功"}
        return HttpResponse(json.dumps(return_data))


def test(request):
    print(request.FILES)
    print(request.POST)
    for data in request.FILES.getlist('idCardPicList'):
        img_path = os.path.join('static/', data.name)
        with open(img_path, 'wb') as fi:
            for i in data.chunks():
                fi.write(i)
    return HttpResponse(1)